package net.wsfcp.android.http.mvp.model;

import android.support.annotation.NonNull;
import android.util.Log;

import com.google.gson.Gson;

import net.wsfcp.android.base.BaseApplication;
import net.wsfcp.android.base.BaseHttpResult;
import net.wsfcp.android.base.BaseModel;
import net.wsfcp.android.bean.requestbean.ReqCityBean;
import net.wsfcp.android.bean.requestbean.ReqCreateAddressBean;
import net.wsfcp.android.bean.requestbean.ReqIdBean;
import net.wsfcp.android.bean.resultbean.AddressBean;
import net.wsfcp.android.bean.resultbean.CityBean;
import net.wsfcp.android.http.UrlHelper;
import net.wsfcp.android.http.exception.ErrorType;
import net.wsfcp.android.http.exception.ExceptionEngine;
import net.wsfcp.android.http.exception.ServerException;
import net.wsfcp.android.http.mvp.subscriber.CommonSubscriber;
import net.wsfcp.android.http.mvp.subscriber.ErrorSubscriber;
import net.wsfcp.android.http.mvp.transformer.CommonTransformer;
import net.wsfcp.android.utils.SpUtils;

import java.util.ArrayList;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by Shaoboyang on 2018/12/14.
 * 收货地址
 */

public class AddressModel extends BaseModel {


    //地址列表
    public void getAddressList(@NonNull final GetAddressListInfoHint infoHint) {

        if (infoHint == null)
            throw new RuntimeException("InfoHint不能为空");
        httpService.getAddressList(UrlHelper.GET_RECEIVE_ADDRESS_LIST + SpUtils.getInstance().getToken()).compose(new CommonTransformer())
                .subscribe(new CommonSubscriber<ArrayList<AddressBean>>(BaseApplication.getContext()) {
                    @Override
                    public void accept(ArrayList<AddressBean> addressBeans) {
                        infoHint.getAddressListSuccessInfo(addressBeans);
                    }

                }, new ErrorSubscriber<Throwable>(BaseApplication.getContext()) {
                    @Override
                    public void accept(Throwable t) {
                        ServerException e = ExceptionEngine.handleException(t);

                        infoHint.getAddressListFailInfo(e.message);


                    }
                });

    }


    //创建地址
    public void createAddress(ReqCreateAddressBean bean, @NonNull final CreateAddressInfoHint
            infoHint) {

        if (infoHint == null)
            throw new RuntimeException("InfoHint不能为空");

        Gson gson = new Gson();
        String obj = gson.toJson(bean);
        Log.e("json数据", obj);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), obj);
        httpService.saveServiceAddress(UrlHelper.SAVE_RECEIVE_ADDRESS + SpUtils.getInstance().getToken(), body)
                .enqueue(new Callback<BaseHttpResult>() {
                    @Override
                    public void onResponse(Call<BaseHttpResult> call, Response<BaseHttpResult> response) {
                        if (response != null && response.body() != null) {
                            if (response.body().getResultCode() == ErrorType.SUCCESS) {
                                infoHint.createAddressSuccessInfo();
                            } else {
                                infoHint.createAddressFailInfo(response.body().getResultMsg());
                            }
                        } else {
                            infoHint.createAddressFailInfo("失败");
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseHttpResult> call, Throwable t) {


                        ServerException e = ExceptionEngine.handleException(t);

                        infoHint.createAddressFailInfo(e.getMessage());
                    }
                });

//                .compose(new CommonTransformer())
//                .subscribe(new CommonSubscriber(BaseApplication.getContext()) {
//                    @Override
//                    public void accept(Object o) {
//                        infoHint.createAddressSuccessInfo();
//                    }
//
//
//                }, new ErrorSubscriber<Throwable>(BaseApplication.getContext()) {
//                    @Override
//                    public void accept(Throwable e) {
//                        if (e instanceof ServerException) {
//
//                            infoHint.createAddressFailInfo(((ServerException) e).message);
//                        }
//
//
//                    }
//                });

    }


    //设置默认地址
    public void setDefaultAddress(String id, @NonNull final SetDefaultAddressInfoHint
            infoHint) {

        if (infoHint == null)
            throw new RuntimeException("InfoHint不能为空");


        ReqIdBean bean = new ReqIdBean(id);//密码需要加密
        Gson gson = new Gson();
        String obj = gson.toJson(bean);
        Log.e("json数据", obj);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), obj);
        httpService.setDefaultAddress(UrlHelper.SET_DEFAULT_RECEIVE_ADDRESS + SpUtils.getInstance().getToken(), body)
                .enqueue(new Callback<BaseHttpResult>() {
                    @Override
                    public void onResponse(Call<BaseHttpResult> call, Response<BaseHttpResult> response) {
                        if (response != null && response.body() != null) {
                            if (response.body().getResultCode() == ErrorType.SUCCESS) {
                                infoHint.setDefaultAddressSuccessInfo();
                            } else {
                                infoHint.setDefaultAddressFailInfo(response.body().getResultMsg());
                            }
                        } else {
                            infoHint.setDefaultAddressFailInfo("失败");
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseHttpResult> call, Throwable t) {
                        ServerException e = ExceptionEngine.handleException(t);
                        infoHint.setDefaultAddressFailInfo(e.getMessage());
                    }
                });

//                .compose(new CommonTransformer())
//                .subscribe(new CommonSubscriber(BaseApplication.getContext()) {
//                    @Override
//                    public void accept(Object o) {
//                        infoHint.setDefaultAddressSuccessInfo();
//                    }
//
//
//                }, new ErrorSubscriber<Throwable>(BaseApplication.getContext()) {
//                    @Override
//                    public void accept(Throwable e) {
//                        if (e instanceof ServerException)
//                            infoHint.setDefaultAddressFailInfo(((ServerException) e).message);
//                    }
//                });

    }


    //删除地址
    public void deleteAddress(String id, @NonNull final DeleteAddressInfoHint
            infoHint) {

        if (infoHint == null)
            throw new RuntimeException("InfoHint不能为空");


        ReqIdBean bean = new ReqIdBean(id);//密码需要加密
        Gson gson = new Gson();
        String obj = gson.toJson(bean);
        Log.e("json数据", obj);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), obj);
        httpService.deleteAddress(UrlHelper.DELETE_RECEIVE_ADDRESS + SpUtils.getInstance().getToken(), body)
                .enqueue(new Callback<BaseHttpResult>() {
                    @Override
                    public void onResponse(Call<BaseHttpResult> call, Response<BaseHttpResult> response) {
                        if (response != null && response.body() != null) {
                            if (response.body().getResultCode() == ErrorType.SUCCESS) {
                                infoHint.deleteAddressSuccessInfo();
                            } else {
                                infoHint.deleteAddressFailInfo(response.body().getResultMsg());
                            }
                        } else {
                            infoHint.deleteAddressFailInfo("失败");
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseHttpResult> call, Throwable t) {

                        ServerException e = ExceptionEngine.handleException(t);
                        infoHint.deleteAddressFailInfo(e.getMessage());
                    }
                });


//                .compose(new CommonTransformer())
//                .subscribe(new CommonSubscriber(BaseApplication.getContext()) {
//                    @Override
//                    public void accept(Object o) {
//                        infoHint.deleteAddressSuccessInfo();
//                    }
//
//
//                }, new ErrorSubscriber<Throwable>(BaseApplication.getContext()) {
//                    @Override
//                    public void accept(Throwable e) {
//                        if(e instanceof ServerException)
//                        infoHint.deleteAddressFailInfo(((ServerException)e).message);
//                    }
//                });

    }


    //获取城市列表
    public void getCity(final ReqCityBean bean, @NonNull final GetCityInfoHint
            infoHint) {

        if (infoHint == null)
            throw new RuntimeException("InfoHint不能为空");

        Gson gson = new Gson();
        String obj = gson.toJson(bean);
        Log.e("json数据", obj);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), obj);
        httpService.getCity(body).compose(new CommonTransformer())
                .subscribe(new CommonSubscriber<ArrayList<CityBean>>(BaseApplication.getContext()) {
                    @Override
                    public void accept(ArrayList<CityBean> cityBeans) {
                        infoHint.getCitySuccessInfo(cityBeans);
                    }


                }, new ErrorSubscriber<Throwable>(BaseApplication.getContext()) {
                    @Override
                    public void accept(Throwable t) {
                        ServerException e = ExceptionEngine.handleException(t);
                        infoHint.getCityFailInfo(e.message, bean.getLevel());

                    }
                });

    }

    //通过接口产生信息回调
    public interface GetAddressListInfoHint {
        void getAddressListSuccessInfo(ArrayList<AddressBean> bean);

        void getAddressListFailInfo(String str);

    }


    //通过接口产生信息回调
    public interface CreateAddressInfoHint {
        void createAddressSuccessInfo();

        void createAddressFailInfo(String str);

    }

    //设置默认地址
    public interface SetDefaultAddressInfoHint {
        void setDefaultAddressSuccessInfo();

        void setDefaultAddressFailInfo(String str);

    }

    //删除地址
    public interface DeleteAddressInfoHint {
        void deleteAddressSuccessInfo();

        void deleteAddressFailInfo(String str);


    }

    //获取城市列表
    public interface GetCityInfoHint {
        void getCitySuccessInfo(ArrayList<CityBean> beans);

        void getCityFailInfo(String str, int type);


    }

}
